package com.baseproject.util;

import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;

/**
 * 字符串加密工具类
 *
 * @author administrator
 * @see [相关类/方法]
 * @since [产品/模块版本]
 */
public class MD5Util {
    /**
     * 对给定的字符串进行md5加密
     *
     * @param pwd 需要加密的字符串
     * @return String [返回类型说明] 加密后的字符串
     * @throws throws [违例类型] [违例说明]
     * @see [类、类#方法、类#成员]
     */
    public static String encode(String pwd) {
        MessageDigest digest;
        try {
            digest = MessageDigest.getInstance("MD5");
            byte[] result = digest.digest(pwd.getBytes());
            StringBuilder sb = new StringBuilder();
            for (byte b : result) {
                int data = (b & 0xff);
                String str = Integer.toHexString(data);
                if (str.length() == 1) {
                    sb.append("0");
                }
                sb.append(str);
            }
            return sb.toString();
        } catch (NoSuchAlgorithmException e) {
            e.printStackTrace();
            // 异常是不会发生的.
            return "";
        }
    }
}
